package com.huawei.petSystemdemo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.utils.MyRequest;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Add_shoppiingadress_Activity extends AppCompatActivity {
    TextView personName;
    TextView tel;
    Spinner province;
    Spinner homeType;
    TextView locationContent;
    private GlobalViewModel global = GlobalViewModel.getGlobal();

    TextView confirm;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_shoppiing_adress);



        personName = findViewById(R.id.person_name);
        tel = findViewById(R.id.tel_number);
        province = findViewById(R.id.spinner_province);
        homeType = findViewById(R.id.spinner_hometype);
        locationContent = findViewById(R.id.location_content);
        confirm = findViewById(R.id.confirm_add);
        confirm.setOnClickListener(v->{
            if(ChickInput()){
                System.out.println("修改");

                Map<String,Object> requestBody = new HashMap<>();


                requestBody.put("username",personName.getText().toString());
                requestBody.put("tel",tel.getText().toString());
                requestBody.put("img",homeType.getSelectedItem().toString());
                requestBody.put("province",province.getSelectedItem().toString());
                requestBody.put("adresscontent",locationContent.getText().toString());
                requestBody.put("user_id", global.user.getId());

                MyRequest.post("/shopadress/addAdress", requestBody, new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        String json = response.body().string();
                        System.out.println(json);
                        JSONObject jsonObject = JSON.parseObject(json);

                    }
                });

                    Intent intent = new Intent(this, ShoppingAdressActivity.class);//界面跳转
                    startActivity(intent);//

            }

        });

    }

    public boolean ChickInput(){
        String StuNumber = personName.getText().toString();
        String StuPwd = tel.getText().toString();
        //trim() 方法用于删除字符串的头尾空白符
        if(StuNumber.trim().equals("")) {
            Toast.makeText(Add_shoppiingadress_Activity.this,"收货人不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(StuPwd.trim().equals("")) {
            Toast.makeText(Add_shoppiingadress_Activity.this,"电话不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    public void onClick (android.view.View back){
        Intent intent = new Intent(this, ShoppingAdressActivity.class);//界面跳转
        startActivity(intent);//

    }
//    public void onClick(View view){
//        Intent intent1 = new Intent(this,DelayMessageService.class);
//        intent1.putExtra(DelayMessageService.EXTRA_MESSAGE,getResources().getString(R.string.SuccessMessage));
//        startService(intent1);
//    }
}